package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("list")
    @ResponseBody
    public List<Permission> list() {
        return permissionService.selectAll();
    }

    @RequestMapping("selectByRoleId")
    @ResponseBody
    public List<Permission> selectByRoleId(Long roleId) {
        return permissionService.selectByRoleId(roleId);
    }

    @RequestMapping("view")
    @RequiresPermissions(value = {"permission:view","权限界面"},logical = Logical.OR)
    public String query(QueryObject qo) {
        return "permission";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult permission(QueryObject qo) {
        return permissionService.query(qo);
    }

    /**
     * 加载权限
     * @return
     */
    @RequestMapping("reload")
    @ResponseBody
    public JSONResult reload() {
        JSONResult result = new JSONResult();
        try {
            permissionService.reload();
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
